// pages/home/home.js
import {
  in_theaters,
  coming_soon,
  selectPreview
} from "../../utils/home/index"
var QQMapWX = require('../../utils/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentIndex:0,
    area:"",
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    background:[
      "/assets/images/home/banner1.jpg",
      "/assets/images/home/banner3.jpg",
      "/assets/images/home/banner3.jpg"
    ],
    ry:[],// 热映
    sy:[],// 上映
    yg:[],// 预告

  },

  /**
   * 生命周期函数--监听页面加载
   */
  getLocation(){
    qqmapsdk = qqmapsdk = new QQMapWX({
      key: '5V3BZ-NYV62-MY6UK-C7LTO-YFB52-VTFIF' // 必填
    });  
    wx.getSetting({
      success: (res) => {
        // res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
        // res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
        // res.authSetting['scope.userLocation'] == true    表示 地理位置授权
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置，请确认授权',
            success: function (res) {
              if (res.cancel) {
                wx.showToast({
                  title: '拒绝授权',
                  icon: 'none',
                  duration: 1000
                })
              } else if (res.confirm) {
                wx.openSetting({
                  success: function (dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      //再次授权，调用wx.getLocation的API
                      
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          //调用wx.getLocation的API
          this.location()
        }
        else {
          //调用wx.getLocation的API
          this.location()
        }
      }
    })
  },
  location(){
    var _this = this;
    wx.getLocation({
      type: 'wgs84',
      success (res) {
        console.log(res)
        const latitude = res.latitude
        const longitude = res.longitude
        const speed = res.speed
        const accuracy = res.accuracy
        qqmapsdk.reverseGeocoder({
          location:{
            latitude: latitude,
            longitude: longitude
          },
          success(res){
            console.log(111111,res);
            _this.setData({
              area:res.result.address_component.city
            })
          } ,fail: function(error) {
            console.error(error);
          },
        })
      }
     })
  },
  swiperEvents(e){
    console.log(e);
    var index = e.detail.current;
    this.setData({
      currentIndex:index
    })
  },
  onShow(){
    this.getLocation()
  },
  onLoad: function (options) {
    
    in_theaters()
    .then(res =>{
      this.setData({
        ry:res.data.list
      })
    });
    coming_soon()
    .then(res =>{
      this.setData({
        sy:res.data.list
      })
    });
    selectPreview()
    .then(res =>{
      this.setData({
        yg:res.data
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})